package com.wlyuan.open.scanner.domain.driver;

import com.wlyuan.open.scanner.dao.entity.DriverDO;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class DriverConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "driverId"),
                @Mapping(target = "birthday", source = "driverBirthday"),
                @Mapping(target = "driverLicenseDate", source = "driverLicenseddate"),
                @Mapping(target = "driverLicenseNo", source = "driverSn"),
                @Mapping(target = "driverLicenseType", source = "driverLicensetype"),
                @Mapping(target = "faceIcon", source = "driverPhoto"),
                @Mapping(target = "gender", source = "driverGender"),
                @Mapping(target = "mobile", source = "driverPhone"),
                @Mapping(target = "name", source = "driverName"),
                @Mapping(target = "driverType", expression = "java(null)"),

                @Mapping(target = "tenantId", source = "driverCompanyid"),
                @Mapping(target = "sourceKey", source = "driverId"),
                @Mapping(target = "createTime", source = "driverInserttime"),
                @Mapping(target = "updateTime", source = "driverUpdatetime"),
                @Mapping(target = "createBy", source = "driverCreator"),
                @Mapping(target = "updateBy", source = "driverUpdater"),
        })
        @BaseMapping
        Driver convert(DriverDO driverDO);

        @AfterMapping
        default void after(@MappingTarget Driver target, DriverDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
            if (target.getCreateBy() == null) {
                target.setCreateBy(0L);
            }
            if (target.getUpdateBy() == null) {
                target.setUpdateBy(0L);
            }
        }

        List<Driver> convert(List<DriverDO> list);

    }

    public static List<Driver> convert(List<DriverDO> creation) {
        return CONVERT.convert(creation);
    }

    public static Driver convert(DriverDO creation) {
        return CONVERT.convert(creation);
    }
}
